define(["require", 'config/appregister','business/login/service/http' , "jquery", "domReady"], function(require, app , httpServices) {
	app.controller('backCtrl', function($scope, $state) {
		$scope.getcodepic = function() {
			getpic();
		}
		$scope.getcodepic2 = function() {
			getpic2();
		}		
		//获取验证码图片
		getpic();
		
		//忘记密码通过手机找回获取图片验证码
		function getpic() {
			var req = {
				type: 'GET',
				uri: 'forgetPasswdByPhoneWithVerifyCode',
			};
			httpServices.somefns(req).success(function(data) {
				$scope.$apply(function() {
					if(data.code == 0) {
						delCookie("verifyToken");
						$scope.codePic1 = data.data.img;
						var arr = data.data.verifyToken;
						var arred = arr.split(",");
						$scope.verifyToken = arred[0];
						var expireTime = arred[1];
						var exp = new Date();
						exp.setTime(exp.getTime() + expireTime);
						document.cookie = "verifyToken=" + $scope.verifyToken + ";expires=" + exp.toGMTString();
					} else {
						alert(data.msg);
					}
				})
			}).error(function(data) {
				alert("验证码请求失败")
			})			
		};
		
		//忘记密码通过邮箱找回获取图片验证码
		function getpic2() {
			var req = {
				type: 'GET',
				uri: 'forgetPasswdByEmailWithVerifyCode',
			};
			httpServices.somefns(req).success(function(data) {
				$scope.$apply(function() {
					if(data.code == 0) {
						delCookie("verifyToken");
						$scope.codePic2 = data.data.img;
						var arr = data.data.verifyToken;
						var arred = arr.split(",");
						$scope.verifyToken = arred[0];
						var expireTime = arred[1];
						var exp = new Date();
						exp.setTime(exp.getTime() + expireTime);
						document.cookie = "verifyToken=" + $scope.verifyToken + ";expires=" + exp.toGMTString();
					} else {
						alert(data.msg);
					}
				})
			}).error(function(data) {
				alert("验证码请求失败")
			})	
		};

		//邮箱1
		$scope.emialback = function() {
			var email = $(".emailtext").val();
			var code = $(".codetext").val().toUpperCase();
			var emailbackurl = "/elink_user/api/user/forgetPasswdByEmailWithSendPhoneCode";
			var thehref = "http://www.gongchengjie.cn/elink/index.html#/emailmodify";
			var req = {
				type:'POST',
				uri:'forgetPasswdByEmailWithSendPhoneCode',
				data:{
					redirectUrl: thehref,
					email: email,
					verifyCode: code,
					verifyToken: $scope.verifyToken
				}
			};
			$.ajax({
				type:"post",
				url:"http://"+allUrl+":8080/elink_user/api/user/forgetPasswdByEmailWithSendPhoneCode",
				data:req.data,
				success:function(data) {
					if(!data)return;
					$scope.$apply(function(){
						if(data.code == 0) {
							sessionStorage.setItem("email", email);
							$state.go("eng.loginedEmail");						
						}else {
							alert(data.msg);
						}
					})
				},
				error:function() {
					alert("忘记密码通过邮箱验证流程二，发送邮箱验证码请求失败");
				}
			});
		};

		//去登录
		$scope.gotologin = function() {
			$state.go("login");
		};

		//提交短信验证码
		$scope.setphonecode = function() {
			setcode();
		};
		
		function setcode() {
			var code = $("#code2").val().toUpperCase();
			var phonenum = $("#phone").val();
			var req = {
				type:'POST',
				uri:'forgetPasswdByPhoneWithCheckPhoneCode',
				data:{
					phone: phonenum,
					verifyCode: code,
					verifyToken: $scope.verifyToken
				}
			};
			httpServices.getemlcodeHttpService(req).success(function(res){
				if(res.code==0){
					$state.go("eng.loginedReset",{"token":$scope.verifyToken});
				}else {
					alert(red.msg);
				}
			}).error(function(res){
					console.log("提交短信验证码请求失败");
			});
		};

		//获取短信验证码
		$scope.getphoneCode = function() {
			var code = $("#code").val().toUpperCase();
			var phonenum = $("#phone").val();
			
			var req = {
				type:'POST',
				uri:'forgetPasswdByPhoneWithSendPhoneCode',
				data:{
					phone: phonenum,
					verifyCode: code,
					verifyToken: $scope.verifyToken
				}
			};
			httpServices.getemlcodeHttpService(req).success(function(res){
					if(!res)return;
					$scope.$apply(function(){
						if(res.code == 0) {
							$('.codebox').unbind("click");
							$(".codebox").html("重新获取(<span id=" + 'time' + ">150</span>s)");
							var timer = $("#time").html();
							var mytime = setInterval(function() {
								if(timer > 0) {
									timer--;
									timer = timer;
								} else {
									clearInterval(mytime);
									$(".codebox").html("重新获取");
									$('.codebox').bind("click");
									getpic();
								}
								$("#time").html(timer);
							}, 1000)
						} else {
							alert(res.msg);
						}						
					});				
			}).error(function(res){
					getpic();
					console.log("找回请求失败");
			});
		};

		//邮箱/手机切换
		$('.back-l').on("click", function() {
			getpic();
			$(this).removeClass("back-ls").addClass("back-l");
			$('#tab-r').removeClass("back-rs").addClass("back-r");
			$('.back-center-r').hide();
			$('.back-center-l').show();

		});
		$('.back-r').on("click", function() {
			getpic2();
			$(this).removeClass("back-r");
			$(this).addClass("back-rs");
			$('.back-l').removeClass("back-l").addClass("back-ls");
			$('.back-center-l').hide();
			$('.back-center-r').show();

		});
		//失焦方法
		$scope.noblur1 = function() {
			var regex_phone = /^1[3|4|5|7|8]\d{9}$/;
			var phone = $("#phone").val();
			if(phone == "") {
				$("#tips1").html("请输入手机号码");
				$("#tips1").show();
			} else if(regex_phone.test(phone)) {
				$("#tips1").hide();
			} else {
				$("#tips1").html("手机号码格式错误");
				$("#tips1").show();
			}

		}
		$scope.noblur2 = function() {
			if($("#code").val() == "") {
				$("#tips2").html("请输入验证码");
				$("#tips2").show();
			}
		}
		$scope.noblur3 = function() {
				if($("#code2").val() == "") {
					$("#tips3").html("请输入短信验证码");
					$("#tips3").show();
				}
			}
			//聚焦方法
		$scope.nofocus1 = function() {
			$("#tips1").hide();
		}
		$scope.nofocus2 = function() {
			$("#tips2").hide();
		}
		$scope.nofocus3 = function() {
				$("#tips3").hide();
			}
		


		$scope.emailfocus = function() {
			$(".tipsemail").hide();
		};
		$scope.emailblur = function() {
			var regex_email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			var usereml = $(".emailtext").val();
			if(usereml == "") {
				$(".tipsemail").show().html("请输入电子邮箱");
			} else if(regex_email.test(usereml)) {
				$(".tipsemail").hide();
			} else {
				$(".tipsemail").show().html("电子邮箱格式错误");
			}
		};

		$scope.codefocus = function() {
			$(".tipscode").hide();
		};
		$scope.codeblur = function() {
			
			var usereml = $(".codetext").val();
			if(usereml == "") {
				$(".tipscode").show().html("请输入验证码");
			} else if(usereml.length == 4) {
				$(".tipscode").hide();
			} else {
				$(".tipscode").show().html("验证码错误，请重新输入");
			}
		};








		function delCookie(name) {
			var exp = new Date();
			exp.setTime(exp.getTime() - 1);
			var cval=getCookie(name);
			if(cval!=null)
			document.cookie= name + "="+cval+";expires="+exp.toGMTString();
		};
		
		function getCookie(name) {
			var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
			if(arr=document.cookie.match(reg))
			return unescape(arr[2]);
			else
			return null;
		};	


	})
})